package com.yufu.screen.orientation;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;

import androidx.appcompat.app.AppCompatActivity;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

import java.util.Objects;

@CapacitorPlugin(name = "Screen")
public class ScreenPlugin extends Plugin {

    // 默认值，由系统决定屏幕方向
    private static final String ANY = "any";
    // 竖屏
    private static final String PORTRAIT_PRIMARY = "portrait-primary";
    private static final String PORTRAIT_SECONDARY = "portrait-secondary";
    // 横屏
    private static final String LANDSCAPE_PRIMARY = "landscape-primary";
    private static final String LANDSCAPE_SECONDARY = "landscape-secondary";
    private static final String PORTRAIT = "portrait";
    private static final String LANDSCAPE = "landscape";
    private final Screen implementation = new Screen();

    @PluginMethod
    public void echo(PluginCall call) {
        String value = call.getString("value");
        JSObject ret = new JSObject();
        ret.put("value", implementation.echo(value));
        call.resolve(ret);
    }

    @PluginMethod
    public void type(PluginCall call) {
        int orientationValue = getContext().getResources().getConfiguration().orientation;
        int settingValue = getActivity().getRequestedOrientation();
        JSObject ret = new JSObject();
        switch (orientationValue) {
            case Configuration.ORIENTATION_LANDSCAPE:
                ret.put("orientation", "landscape");
                break;
            case Configuration.ORIENTATION_PORTRAIT:
                ret.put("orientation", "portrait");
                break;
            case Configuration.ORIENTATION_SQUARE:
            case Configuration.ORIENTATION_UNDEFINED:
                ret.put("orientation", "");
                break;
        }
        switch (settingValue) {
            case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
                ret.put("setting", ANY);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
                ret.put("setting", LANDSCAPE_PRIMARY);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
                ret.put("setting", PORTRAIT_PRIMARY);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
                ret.put("setting", LANDSCAPE);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
                ret.put("setting", PORTRAIT);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
                ret.put("setting", LANDSCAPE_SECONDARY);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
                ret.put("setting", PORTRAIT_SECONDARY);
                break;
            case ActivityInfo.SCREEN_ORIENTATION_BEHIND:
            case ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR:
            case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
            case ActivityInfo.SCREEN_ORIENTATION_LOCKED:
            case ActivityInfo.SCREEN_ORIENTATION_NOSENSOR:
            case ActivityInfo.SCREEN_ORIENTATION_SENSOR:
            case ActivityInfo.SCREEN_ORIENTATION_USER:
            case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
            case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
                ret.put("setting", settingValue);
                break;
        }
        call.resolve(ret);
    }

    @SuppressLint("SourceLockedOrientationActivity")
    @PluginMethod
    public void lock(PluginCall call) {
        String setting = call.getString("setting", ANY);
        AppCompatActivity activity = getActivity();
        switch (Objects.requireNonNull(setting)) {
            case LANDSCAPE_PRIMARY:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case PORTRAIT_PRIMARY:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case LANDSCAPE:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                break;
            case PORTRAIT:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                break;
            case LANDSCAPE_SECONDARY:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            case PORTRAIT_SECONDARY:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            default:
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        }
        call.resolve();
    }
}
